package com.zhz.common.tool.sms.autoconfig;

import com.zhz.common.tool.sms.SmsSender;
import com.zhz.common.tool.sms.enums.SmsSendTypeEnum;
import com.zhz.common.tool.sms.properties.SmsProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Objects;

/**
 * 手机短信配置
 *
 * @author Alfred
 */
@Slf4j
@Configuration
@ConditionalOnProperty(prefix = "ihrm.sms", name = {"type"}, matchIfMissing = false)
@EnableConfigurationProperties(SmsProperties.class)
public class SmsSenderAutoConfiguration {

    /**
     * 创建sms短信发送
     *
     * @param smsProperties sms配置
     * @return
     */
    @Bean
    @ConditionalOnMissingBean
    public SmsSender smsSender(SmsProperties smsProperties) {
        if (Objects.isNull(smsProperties.getType())) {
            throw new RuntimeException("短信发送类型不能为空,具体选择类型可以看SmsSendTypeEnum枚举类");
        }
        SmsSendTypeEnum smsSendTypeEnum = SmsSendTypeEnum.getByCode(smsProperties.getType());
        if (Objects.isNull(smsSendTypeEnum)) {
            throw new RuntimeException("短信发送类型不存在,具体选择类型可以看SmsSendTypeEnum枚举类");
        }
        return smsSendTypeEnum.create(smsProperties);
    }
}
